package com.mcy.springbatch.job;

import com.mcy.springbatch.entity.SysUser;
import com.mcy.springbatch.job.BaseJobConfig;
import com.mcy.springbatch.reader.ReadUserDataMultiFlatFileItemReader;
import com.mcy.springbatch.writer.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 对多个文件进行读写
 * 利用MultiResourceItemReader进行对多个文件进行读取
 * 利用CompositeItemWriter和ClassifierCompositeItemWriter写出数据到多个文件
 *  CompositeItemWriter：同时写多个文件
 *  ClassifierCompositeItemWriter：可以根据分类写出数据到多个文件
 * @author : Create By mcy
 * @date : 2019/8/25 15:46:40
 */

@Slf4j
@Configuration
public class IMultiReadWriteDataWithFileJobConfig extends BaseJobConfig {

    //读多个文件的reader
    @Autowired
    ReadUserDataMultiFlatFileItemReader readUserDataMultiFlatFileItemReader;

    //不按数据分类写多个文件的writer
    @Autowired
    SysUserCompositeItemWriter compositeItemWriter;

    //按数据分类写多个文件的writer
    @Autowired
    SysUserClassifierCompositeItemWriter classifierCompositeItemWriter;

    @Autowired
    SysUserFlatFileItemWriter sysUserFlatFileItemWriter;

    @Autowired
    SysUserStaxEventItemWriter sysUserStaxEventItemWriter;

    @Bean
    public Job flatFileWriterJob() {
        return jobBuilderFactory.get("flatFileWriterJob")
                .incrementer(new RunIdIncrementer())
                .start(flatFileWriterStep())
                .build();
    }

    @Bean
    public Step flatFileWriterStep() {
        return stepBuilderFactory.get("flatFileWriterStep")
                .<SysUser, SysUser>chunk(20)
                .reader(readUserDataMultiFlatFileItemReader)

//                .writer(compositeItemWriter)

                .writer(classifierCompositeItemWriter)//由于ClassifierCompositeItemWriter没有实现ItemStreamWriter，故需把sysUserFlatFileItemWriter和sysUserStaxEventItemWriter加入到stream
                .stream(sysUserFlatFileItemWriter)
                .stream(sysUserStaxEventItemWriter)
                .build();
    }


}
